package com.contractpage.ec.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.contractpage.common.persistence.Page;
import com.contractpage.ec.dao.ContractDetailDao;
import com.contractpage.ec.dao.ContractInboundDao;
import com.contractpage.ec.dao.ContractInboundHistroyDao;
import com.contractpage.ec.entity.ContractDetail;
import com.contractpage.ec.entity.ContractInbound;
import com.contractpage.ec.entity.ContractInboundHistroy;
import com.contractpage.ec.service.ContractInboundHistroyService;



/**
 * 
 * ContractInboundHistroyServiceImpl数据库操作接口类
 * 
 **/
@Transactional
@Service
public class ContractInboundHistroyServiceImpl implements ContractInboundHistroyService{
	@Autowired
	private ContractInboundHistroyDao contractInboundHistroyDao;
	@Autowired
	private ContractDetailDao contractDetailDao;
	@Autowired
	private ContractInboundDao contractInboundDao;
	@Override
	public Page<ContractInboundHistroy> findList(Page<ContractInboundHistroy> page,ContractInboundHistroy entity){
		entity.setPage(page);
		page.setList(contractInboundHistroyDao.findList(entity));
		return page;
	}
	@Override
	public int insert(ContractInboundHistroy entity){
		entity.preInsert("");
		return contractInboundHistroyDao.insert(entity);
	}
	@Override
	public int update(ContractInboundHistroy entity){
		entity.preUpdate();
		return contractInboundHistroyDao.update(entity);
	}
	@Override
	public int delete(ContractInboundHistroy entity){
		int i = 0;
		ContractDetail detail = new ContractDetail();
		detail.setBatch(entity.getBatch());
		detail.setInboundid(entity.getInboundid());
		List<ContractDetail> details = contractDetailDao.findList(detail);
		for(ContractDetail contractDetail : details){
			if(!"0".equals(contractDetail.getStatus())){
				return 0;
			}
		}
		ContractInbound inbound = new ContractInbound();
		inbound.setId(entity.getInboundid());
		inbound = contractInboundDao.get(inbound);
		inbound.setTotal(inbound.getTotal() - entity.getAmount());
		contractInboundDao.update(inbound);
		contractDetailDao.delete(detail);
		i = contractInboundHistroyDao.delete(entity);
		return i;
	}
}